package question1;

/**
 * 编写Java代码实现一个计数器类 Counter
 *
 * @FileName Counter
 * @Description
 * @Author Sword
 * @date 2024-10-11
 **/

public class CounterTest {
    public static void main(String[] args) {
        Counter counter = new Counter();

        counter.increment();
        System.out.println("计数器加一：=" + counter.getCounterValue());


        counter.decrement();
        System.out.println("计数器减一: =" + counter.getCounterValue());

        counter.reset();
        System.out.println("计数器清零: =" + counter.getCounterValue());

    }
}


 class Counter {
    //包含域 counterValue用来保存计数器的当前数值
    private int counterValue;

    //方法increment() 使计数器加一
    public void increment() {
        counterValue++;
    }

    // 方法 decrement() 使计数器减一
    public void decrement() {
        counterValue--;
    }

    // 方法 reset() 使计数器清零。
    public void reset() {
        counterValue = 0;
    }

    public int getCounterValue() {
        return counterValue;
    }

    public void setCounterValue(int counterValue) {
        this.counterValue = counterValue;
    }
}
